﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Linq;
using Business_Object;
using Models;

namespace BusinessService
{
    public partial class TestSave : System.Web.UI.Page
    {
        private DiaDiemService diaDiemService = new DiaDiemService(); 
        private TaiKhoanService taiKhoanService =new TaiKhoanService();
        protected void Page_Load(object sender, EventArgs e)
        {
            String action = Request.QueryString["action"];
            if (Session["username"] != null)
            {
                String username = Session["username"].ToString();
                TaiKhoan taiKhoan = taiKhoanService.FindByUserName(username);

                if (action.Equals("Them"))
                {
                    String name = Request["name"];
                    String address = Request["address"];
                    String description = Request["description"];
                    int type = int.Parse(Request["type"]);
                    double lat = double.Parse(Request["lat"]);
                    double lng = double.Parse(Request["lng"]);


                    DiaDiem diaDiem = new DiaDiem();
                    diaDiem.TenDiaDiem = name;
                    diaDiem.DiaChi = address;
                    diaDiem.MieuTa = description;
                    diaDiem.ViDo = lat;
                    diaDiem.KinhDo = lng;
                    diaDiem.MaDichVu = type;
                    diaDiem.NgayTao = DateTime.Now;
                    diaDiem.MaTaiKhoan = taiKhoan.MaTaiKhoan;
                    diaDiem.QuocGia = "Unknow";

                    diaDiemService.Insert(diaDiem);
                    LoadFile();

                    Response.Redirect("MyPlaceFavorite.aspx");
                }
                else if (action.Equals("Xoa"))
                {
                    int id = int.Parse(Request["id"]);
                    DiaDiem diaDiemResult = diaDiemService.Find(id);
                    if (diaDiemResult.MaDiaDiem == id)
                    {
                        diaDiemService.Delete(diaDiemResult.MaDiaDiem);
                        LoadFile();
                    }
                    action = "";
                }
                else if (action.Equals("Sua"))
                {
                    int id = int.Parse(Request["id"]);
                    String name = Request["name"];
                    String address = Request["address"];
                    String description = Request["description"];
                    int type = int.Parse(Request["type"]);
                    //double lat = double.Parse(Request["lat"]);
                    //double lng = double.Parse(Request["lng"]);

                    DiaDiem diaDiemResult = diaDiemService.Find(id);
                    if (diaDiemResult.MaDiaDiem == id)
                    {
                        DiaDiem diaDiem = new DiaDiem();
                        diaDiem.MaDiaDiem = diaDiemResult.MaDiaDiem;
                        if (ValidateObject(name))
                        {
                            diaDiem.TenDiaDiem = name;
                        }
                        else
                        {
                            diaDiem.TenDiaDiem = diaDiemResult.TenDiaDiem;
                        }
                        if (ValidateObject(address))
                        {
                            diaDiem.DiaChi = address;
                        }
                        else
                        {
                            diaDiem.DiaChi = diaDiemResult.DiaChi;
                        }
                        if (ValidateObject(description))
                        {
                            diaDiem.MieuTa = description;
                        }
                        else
                        {
                            diaDiem.MieuTa = diaDiemResult.MieuTa;
                        }
                        diaDiem.ViDo = diaDiemResult.ViDo;
                        diaDiem.KinhDo = diaDiemResult.KinhDo;
                        diaDiem.MaDichVu = type;
                        diaDiem.NgayTao = DateTime.Now;
                        diaDiem.MaTaiKhoan = taiKhoan.MaTaiKhoan;
                        diaDiem.QuocGia = diaDiemResult.QuocGia;

                        diaDiemService.Update(diaDiem);
                        LoadFile();
                    }
                }
            }


        }
        private void LoadFile()
        {
            String filePath = HttpContext.Current.Server.MapPath("~/MyPlace/DiaDiems.xml");

            DiaDiemService diemService = new DiaDiemService();
            XmlParser xmlParser = new XmlParser();
            XmlDocument doc = xmlParser.ConvertObjectToXml(diemService.All());
            doc.Save(filePath);
        }
        private bool ValidateObject(String name)
        {
            if (String.IsNullOrEmpty(name) || name == "")
                return false;

            return true;
        }
       
    }
}
